package com.admin.controller;

import com.admin.model.SensitiveWords;
import com.admin.service.SensitiveWordsService;
import com.alibaba.nacos.api.exception.NacosException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/sensitiveWords")
public class SensitiveWordsController {

    @Autowired
    private SensitiveWordsService sensitiveWordsService; // 假设这是你的服务层接口

    @PostMapping
    public ResponseEntity<String> insertSensitiveWord(@RequestBody SensitiveWords sensitiveWords) {
        try {
            sensitiveWordsService.insertSensitiveWord(sensitiveWords);
            return ResponseEntity.ok("敏感词添加成功");
        } catch (NacosException e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("添加敏感词时发生错误");
        }
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<String> deleteSensitiveWordById(@PathVariable("id") Integer id) {
        try {
            SensitiveWords sensitiveWords = new SensitiveWords();
            sensitiveWords.setId(id);
            sensitiveWordsService.deleteSensitiveWordById(sensitiveWords);
            return ResponseEntity.ok("敏感词删除成功");
        } catch (NacosException e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("删除敏感词时发生错误");
        }
    }

    @GetMapping("/page")
    public ResponseEntity<PageInfo<SensitiveWords>> selectSensitiveWordsByPage(
            @RequestParam("pageNum") int pageNum,
            @RequestParam("pageSize") int pageSize) {
        PageInfo<SensitiveWords> pageInfo = sensitiveWordsService.selectSensitiveWordsByPage(pageNum, pageSize);
        return ResponseEntity.ok(pageInfo);
    }

}

